import React from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import { urlParse } from '@/utils/utils';
import BatchUpload from '../../components/BatchUpload/Index';
import StepItem from '../../components/BatchUpload/StepItem';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';
import SelectProd from './SelectProd';

@connect(({ batchadjust, publicdata, loading }) => ({
  uploading: loading.effects['batchadjust/uploadFileWithProgress'],
  batchQuery: batchadjust.batchQuery,
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: batchadjust.currentStep,
  progressPercent: batchadjust.progressPercent,
  resultInfo: batchadjust.resultInfo,
  showProgress: batchadjust.showProgress,
}))
class Batch extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      batchAdjType: '1',
    };
  }

  componentDidMount() {
    const { location } = this.props;
    const { batchAdjType } = urlParse(location.search);
    this.clearModel();
    this.setState({
      batchAdjType,
    });
    // dispatch({
    //   type: 'batchadjust/updateQuery',
    //   payload: { batchAdjType },
    // });
  }

  componentWillUnmount() {
    this.clearModel();
  }

  // 重置model
  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchadjust/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
        batchQuery: {
          sendSuppId: undefined,
          contId: undefined,
          custId: undefined,
          quoId: undefined,
          pdId: undefined, // 产品id
          packNo: undefined, // 包id
          pdGroupId: undefined, // 组合包id
        },
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'batchadjust/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      dispatch({
        type: 'batchadjust/updateModelData',
        payload: {
          currentStep: 0,
          progressPercent: 0,
          showProgress: false,
        },
      });
    }
  };

  // 下一步
  onNext = () => {
    const { dispatch, currentStep } = this.props;
    const { batchAdjType } = this.state;
    dispatch({
      type: 'batchadjust/getDownloadInfo',
      payload: {
        batchAdjType,
      },
    });
    this.setCurrentstep(currentStep + 1);
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const {
      batchQuery: { sendSuppId, contId, custId, quoId, pdId, packNo },
      dispatch,
    } = this.props;
    const uploadQuery = {
      sendSuppId,
      contId,
      custId,
      quoId,
      pdId,
      packNo,
    };
    const { batchAdjType } = this.state;
    // const { batchAdjType } = urlParse(location.search);
    // 需要携带参数即传：batchQuery，不需要传参可省略
    Object.entries(uploadQuery).forEach(([key, value]) => {
      formData.append(key, value);
    });
    dispatch({
      type: 'batchadjust/uploadFileWithProgress',
      payload: {
        params: formData,
        batchAdjType,
      },
    });
  };

  render() {
    const {
      fileId,
      fileName,
      uploading,
      batchQuery,
      location,
      dispatch,
      currentStep,
      showProgress,
      resultInfo,
      progressPercent,
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <StepItem title="选择产品">
            <SelectProd
              batchQuery={batchQuery}
              onNext={value => this.onNext(value)}
              location={location}
            />
          </StepItem>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: { fileId, fileName },
              url: '/csApi/batchTask/downloadFile',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            progressPercent={progressPercent || 0}
            progressTitle="批量调整任务"
            progressVisible={showProgress}
            preStep={() => {
              this.setCurrentstep(currentStep - 1);
              dispatch({
                type: 'batchadjust/updateModelData',
                payload: {
                  isGoBack: true,
                },
              });
              router.push(
                `/customservicemanage/adjusttask/batchservicefee?batchAdjType=${
                  batchQuery.batchAdjType
                }`
              );
            }}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
